<template>
  <TaskCalendar
    :events="events"
    :finish-events="finishEvents"
    :no-time-events="noTimeEvents"
    :key="calendarKey"
  ></TaskCalendar>
</template>

<script lang="ts" setup>
import TaskCalendar from "@/components/common/TaskCalendar.vue";
import { FinishStage, FinishStageList } from "@model/StageFinish";
import { StateMoal } from "@model/StateMoal";
import { TaskData } from "@model/TaskList";
import { StageList, TaskIdsModal } from "@model/stage";
import UseCacheFinishStageMap from "@pinias/UseCacheFinishStageMap";
import UseStage from "@pinias/UseStage";
import UseSurfaceSetting from "@pinias/UseSurfaceSetting";
import useTask from "@pinias/UseTask";
import pinia from "@pinias/pinia";
import { getstartOfDay, timestampToDateTime } from "@utils/DateUtils";
import { onMounted, ref, watch } from "vue";

// 全局数据
const { getSurfaceSetting } = UseSurfaceSetting();
const surfaceSetting = getSurfaceSetting();
const { getStageList } = UseStage(pinia);
const stageList: StageList = getStageList();
const { getCacheFinishStageMap } = UseCacheFinishStageMap(pinia);
const cacheFinishStageMap = getCacheFinishStageMap();
const { getTaskList } = useTask(pinia);
const taskList = getTaskList();

// 当前页面数据
const calendarKey = ref(new Date().valueOf());
let events = new Array<{ title: string; start: string; end: string; color: string; description: string }>();
let finishEvents = new Array<{ title: string; start: string; end: string; color: string; description: string }>();
let noTimeEvents = new Array<{ title: string; start: string; end: string; color: string; description: string }>();
const putEvent = (oldDdata: TaskData, finish: boolean, color: string) => {
  if (oldDdata !== undefined) {
    if (oldDdata.timeMentionModal.deadLine !== null && oldDdata.timeMentionModal.deadLine > 0) {
      const ddl = timestampToDateTime(oldDdata.timeMentionModal.deadLine);
      if (oldDdata.timeMentionModal.deadLine <= new Date().getTime() && color === "yellow") color = "red";
      const e = {
        title: oldDdata.name,
        start: ddl,
        end: ddl,
        color: color,
        description: oldDdata.desc,
      };
      if (finish) finishEvents.push(e);
      else events.push(e);
      // console.log(oldDdata,e)
    } else {
      if (!finish) {
        const ddl = timestampToDateTime(getstartOfDay(new Date().valueOf()));
        const e = {
          title: oldDdata.name,
          start: ddl,
          end: ddl,
          color: "black",
          description: oldDdata.desc,
        };
        noTimeEvents.push(e);
      }
    }
  }
};
const putStateModlEvent = (stateMoal: StateMoal) => {
  if (stateMoal !== undefined) {
    // 进行中
    let set = new Set<number>();
    StateMoal.getAllId(stateMoal, set, true, false, false);
    let list = [...set];
    for (let i = 0; i < list.length; i++) {
      const oldDdata = taskList.getTaskDataById(list[i]);
      if (oldDdata !== undefined) putEvent(oldDdata, false, "yellow");
    }
    // 未进行
    set = new Set<number>();
    StateMoal.getAllId(stateMoal, set, false, true, false);
    list = [...set];
    //console.log(taskState,showStage,satgeData,stateMoal,list)
    for (let i = 0; i < list.length; i++) {
      const oldDdata = taskList.getTaskDataById(list[i]);
      if (oldDdata !== undefined) putEvent(oldDdata, false, "purple");
    }
    // 已结束
    set = new Set<number>();
    StateMoal.getAllId(stateMoal, set, false, false, true);
    list = [...set];
    //console.log(taskState,showStage,satgeData,stateMoal,list)
    for (let i = 0; i < list.length; i++) {
      const oldDdata = taskList.getTaskDataById(list[i]);
      if (oldDdata !== undefined) putEvent(oldDdata, true, "gray");
    }
  }
};
const getEvents = async () => {
  const showStage = surfaceSetting.getShowStage();
  const satgeData = stageList.getStageDataById(showStage);
  events = [];
  finishEvents = [];
  noTimeEvents = [];
  if (satgeData !== undefined) {
    if (satgeData.finished) {
      const fieldId = satgeData.fatherId;
      const finishStageList = await cacheFinishStageMap.getFinishStageList(fieldId);
      const finishStage = FinishStageList.getFinishStage(finishStageList, satgeData.id);
      if (finishStage !== undefined) {
        const taskDatas = FinishStage.getTaskDataList(finishStage, 3);
        for (let i = 0; i < taskDatas.length; i++) {
          const oldDdata = taskDatas[i];
          putEvent(oldDdata, true, "gray");
        }
      }
    } else {
      // 未结束阶段
      let stateMoal = TaskIdsModal.getStateModal4Task(satgeData.task, 1);
      putStateModlEvent(stateMoal);
      stateMoal = TaskIdsModal.getStateModal4Task(satgeData.task, 2);
      putStateModlEvent(stateMoal);
      // console.log("++++++++++++++++",stateMoal)
    }
  }
  calendarKey.value = new Date().valueOf();
};
onMounted(getEvents);
watch(surfaceSetting.showStage, getEvents);
</script>
